﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using Newtonsoft.Json;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.MemberCard
{
    public partial class MemberCardDetail : System.Web.UI.Page
    {
        public WxMemberCardRight cardRight { get; set; }
        //public WxMemberCard card { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    WxMemberCardConfig config = session.CreateCriteria<WxMemberCardConfig>().Add(Expression.Eq("OwnerAccount", "admin")).UniqueResult<WxMemberCardConfig>();
                    ddl_member_card_item.DataSource = session.CreateCriteria<WxMemberCardRule>().Add(Expression.Eq("ConfigId", config.Id)).List<WxMemberCardRule>();
                    ddl_member_card_item.DataTextField = "Title";
                    ddl_member_card_item.DataValueField = "Id";
                    ddl_member_card_item.DataBind();

                    if (!string.IsNullOrEmpty(Request["cardno"]))
                    {
                        ViewState["cardno"] = Request["cardno"];
                    }
                    //card= session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("MemberAccount", Request["acc"])).UniqueResult<WxMemberCard>();
                    ParamKit.Bind("MemberCardKind", ddl_member_card_kind, null, null, null, "", "normalcard");
                    //设置对象
                    int uid = 0;
                    if (int.TryParse(Request["uid"], out uid))
                    {
                        cardRight = session.Get<WxMemberCardRight>(uid);

                        if (cardRight != null)
                        {
                            //保存uid
                            ViewState["uid"] = uid;
                            //初始化页面内容
                            txt_decs.Value = cardRight.Description;
                            txt_enddate.Text = string.Format("{0:yyyy-MM-dd}", cardRight.EndDate);
                            txt_startdate.Text = string.Format("{0:yyyy-MM-dd}", cardRight.StartDate); 
                            txt_balance.Text = cardRight.RemainAmount.ToString();
                            txt_times.Text = cardRight.RemainAmount.ToString();
                            txt_title.Text = cardRight.Title;
                            txt_card_no.Text = cardRight.CardNo;

                            ddl_member_card_item.SelectedValue = cardRight.RuleId.ToString();

                            ddl_member_card_kind.SelectedValue = cardRight.Kind;
                        }
                    }
                    else
                    {
                        txt_enddate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                        txt_startdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    }
                }
            }
            else
            {
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode();

                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    WxMemberCardRule rule = session.CreateCriteria<WxMemberCardRule>().Add(Expression.Eq("Id", int.Parse(ddl_member_card_item.SelectedValue))).UniqueResult<WxMemberCardRule>();
                    if (ViewState["uid"] == null || ViewState["uid"] == "")
                    {
                        cardRight = new WxMemberCardRight();
                        session.Save(cardRight);
                        cardRight.SubCardNo = string.Format("{0}{1:D5}", rule.HeadChar, cardRight.Id);
                        cardRight.RuleId = int.Parse(ddl_member_card_item.SelectedValue);
                     
                    }
                    else
                    {
                        cardRight = session.Get<WxMemberCardRight>(ViewState["uid"]);
                    }
                    WxMemberCard card =session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("CardNo", txt_card_no.Text)).UniqueResult<WxMemberCard>();
                    if (card != null)
                    {
                        cardRight.CardNo = txt_card_no.Text;
                        cardRight.MemberAccount = card.CardNo;
                    }
                    else {
                        rc.statusCode = 300;
                            rc. message = "会员卡号不存在";
                      
                        _framePage.Alert(rc);
                        return;
                    }
                    
                    cardRight.Description = txt_decs.Value;
                    if (ddl_member_card_kind.SelectedValue == "timescard")
                    {
                        cardRight.RemainAmount = decimal.Parse(txt_times.Text);
                    }
                    else
                    {
                        cardRight.RemainAmount = decimal.Parse(txt_balance.Text);
                    }
                    cardRight.Title = txt_title.Text;
                    cardRight.StartDate = DateTime.Parse(txt_startdate.Text);
                    cardRight.EndDate = DateTime.Parse(txt_enddate.Text);
                    cardRight.Description = txt_decs.Value;
                    cardRight.Kind = ddl_member_card_kind.SelectedValue;
                    cardRight.RuleId =int.Parse( ddl_member_card_item.SelectedValue);

                    session.SaveOrUpdate(cardRight);
                    session.Flush();
                }


                //框架返回成功提示
                
              rc.statusCode = 200;
                    rc.message = "操作成功";


                    rc.callbackType = "closeCurrent";
                //rc.forwardUrl = Request.Path+"?cardno="+cardRight.Id;

                _framePage.Alert(rc);
            }
        }
    }
}